﻿using SmallJob.Model;
using System;
using System.Text;

namespace SmallShop.Services.Code
{
    public class VerifyAccount
    {
        public const int NUMBER_STEP = 5;
        public const int STEP_EMAIL = 0;
        public const int STEP_FB = 1;
        public const int STEP_AVATAR = 2;
        public const int STEP_NAME = 3;
        public const int STEP_PHONE = 4;

        private string VerifyStatus { get; set; }

        public VerifyAccount()
        {
            this.VerifyStatus = null;
        }

        public VerifyAccount(string VerifyStatus)
        {
            if (CheckFormat(VerifyStatus))
            {
                this.VerifyStatus = VerifyStatus;
            }
            else
            {
                throw new Exception("Wrong Format!");
            }
        }

        public VerifyAccount(string VerifyStatus, bool defaultIfWrong)
        {
            if (CheckFormat(VerifyStatus))
            {
                this.VerifyStatus = VerifyStatus;
            }
            else
            {
                if (defaultIfWrong)
                {
                    this.VerifyStatus = GetDefaultVerifyCode();
                }
            }
        }

        public string GetVerifyStatus()
        {
            return VerifyStatus;
        }

        public bool CheckFormat(string toCheck)
        {
            for (int i = 0; i < NUMBER_STEP; i++)
            {
                try
                {
                    if (toCheck[i] != '0' && toCheck[i] != '1')
                    {
                        return false;
                    }
                }
                catch (Exception)
                {
                    return false;
                }
            }
            return true;
        }

        public string GetDefaultVerifyCode()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < NUMBER_STEP; i++)
            {
                sb.Append("0");
            }
            return sb.ToString();
        }

        public bool GetStatusOfStep(int step)
        {
            try
            {
                if (VerifyStatus[step] == '1') return true;
                else return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public void SetStatusOfStep(int step, bool isdone)
        {
            if (step >= NUMBER_STEP)
            {
                VerifyStatus = GetDefaultVerifyCode();
            }
            char newstatus = isdone ? '1' : '0';
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < NUMBER_STEP; i++)
            {
                if (i == step)
                {
                    sb.Append(newstatus);
                }
                else
                {
                    sb.Append(VerifyStatus[i]);
                }
            }
            VerifyStatus = sb.ToString();
        }

        public bool IsExclude(int step)
        {
            switch (step)
            {
                case 2: return true;
                default: return false;
            }
        }

        public bool IsDoneAll()
        {
            for (int i = 0; i < NUMBER_STEP; i++)
            {
                if (!IsExclude(i))
                {
                    var status = GetStatusOfStep(i);
                    if (!status) return false;
                }
            }
            return true;
        }

        public string GetPercentDone()
        {
            double count = 0;
            var total = 0;
            for (int i = 0; i < NUMBER_STEP; i++)
            {
                if (!IsExclude(i))
                {
                    total = total + 1;
                    if (VerifyStatus[i] == '1')
                    {
                        count = count + 1;
                    }
                }
            }

            var completed = Math.Round((count / total), 2) * 100;
            var output = completed.ToString();
            return output;
        }

        public void CreateStatusByUSer(User user)
        {
            if (!string.IsNullOrWhiteSpace(user.FbID))
            {
                SetStatusOfStep(STEP_FB, true);
            }
            if (!string.IsNullOrWhiteSpace(user.Avatar))
            {
                SetStatusOfStep(STEP_AVATAR, true);
            }
            if (!string.IsNullOrWhiteSpace(user.Phone))
            {
                SetStatusOfStep(STEP_PHONE, true);
            }
        }

        public bool IsSameDone()
        {
            var email = GetStatusOfStep(STEP_EMAIL);
            var fb = GetStatusOfStep(STEP_FB);
            var name = GetStatusOfStep(STEP_NAME);
            var phone = GetStatusOfStep(STEP_PHONE);
            if (email && name && phone) return true;
            if (fb) return true;
            return false;
        }
    }
}